<template>
  <div class="register-container">
    <div class="register-box">
      <!-- 左侧装饰 -->
      <div class="register-aside">
        <div class="aside-content">
          <h1>人员信息管理系统</h1>
          <p>高效、便捷的人员管理解决方案</p>
          <div class="decoration">
            <i class="el-icon-user-solid"></i>
          </div>
        </div>
      </div>
      
      <!-- 右侧注册表单 -->
      <div class="register-main">
        <div class="register-form">
          <h2>账号注册</h2>
          <p class="sub-title">创建一个新的账号</p>
          
          <el-form
            ref="formRef"
            :model="registerForm"
            :rules="rules"
            @submit.prevent="handleRegister"
          >
            <el-form-item prop="username">
              <el-input
                v-model="registerForm.username"
                prefix-icon="el-icon-user"
                placeholder="请输入用户名"
                clearable
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请输入密码"
                show-password
              />
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请确认密码"
                show-password
              />
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                prefix-icon="el-icon-message"
                placeholder="请输入邮箱"
                clearable
              />
            </el-form-item>
            
            <el-form-item class="register-btn">
              <el-button
                type="primary"
                native-type="submit"
                :loading="loading"
                round
                class="submit-btn"
              >
                注册
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="form-footer">
            <span>已有账号？</span>
            <el-button
              type="text"
              @click="$router.push('/login')"
            >
              立即登录
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Register',
  setup() {
    const store = useStore()
    const router = useRouter()
    const formRef = ref(null)
    const loading = ref(false)
    
    const registerForm = ref({
      username: '',
      password: '',
      confirmPassword: '',
      email: ''
    })

    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (registerForm.value.confirmPassword !== '') {
          formRef.value.validateField('confirmPassword')
        }
        callback()
      }
    }

    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== registerForm.value.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, validator: validatePass, trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, validator: validatePass2, trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ]
    }

    const handleRegister = async () => {
      if (!formRef.value) return
      
      try {
        await formRef.value.validate()
        loading.value = true
        
        const { confirmPassword, ...userData } = registerForm.value
        await store.dispatch('register', userData)
        
        ElMessage.success('注册成功')
        router.push('/login')
      } catch (error) {
        if (error.response && error.response.data) {
          ElMessage.error(error.response.data.message || '注册失败')
        } else {
          ElMessage.error('注册失败：' + (error.message || '未知错误'))
        }
      } finally {
        loading.value = false
      }
    }

    return {
      registerForm,
      loading,
      rules,
      formRef,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
}

.register-box {
  display: flex;
  width: 900px;
  height: 600px; /* 比登录框稍高 */
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.register-aside {
  width: 400px;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 40px;
  display: flex;
  align-items: center;
  color: white;
  position: relative;
}

.aside-content {
  text-align: center;
  width: 100%;
}

.aside-content h1 {
  font-size: 28px;
  margin-bottom: 20px;
  font-weight: 500;
}

.aside-content p {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 40px;
}

.decoration {
  font-size: 120px;
  opacity: 0.2;
  margin-top: 40px;
}

.register-main {
  flex: 1;
  padding: 40px;
  display: flex;
  align-items: center;
}

.register-form {
  width: 100%;
  padding: 0 40px;
}

.register-form h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
  text-align: center;
}

.sub-title {
  font-size: 14px;
  color: #909399;
  text-align: center;
  margin-bottom: 30px;
}

:deep(.el-input__inner) {
  height: 45px;
  line-height: 45px;
}

:deep(.el-input__prefix) {
  font-size: 18px;
}

.submit-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
}

.form-footer {
  margin-top: 20px;
  text-align: center;
  color: #606266;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-button--text) {
  font-size: 14px;
  padding: 0 4px;
  color: #1890ff;
}

/* 响应式设计 */
@media screen and (max-width: 992px) {
  .register-box {
    width: 100%;
    height: 100vh;
    border-radius: 0;
  }
  
  .register-aside {
    display: none;
  }
  
  .register-main {
    padding: 20px;
  }
  
  .register-form {
    padding: 0 20px;
  }
}

@media screen and (max-width: 576px) {
  .register-form {
    padding: 0;
  }
}

/* 添加一些动画效果 */
.register-box {
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 添加输入框焦点效果 */
:deep(.el-input__inner:focus) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

/* 优化错误提示样式 */
:deep(.el-form-item__error) {
  padding-top: 4px;
  font-size: 13px;
}

/* 添加按钮悬停效果 */
.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.submit-btn {
  transition: all 0.3s ease;
}
</style> 